package com.hgcode.wtboot.mq;

import cn.hutool.core.map.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @Desc ：Rocketmq consumer 延时启动配置
 * @author ：wentao_tang
 * @date ：2023/2/13 14:39
**/
//@Component
@Slf4j
public class MqDelayStartConfig implements ApplicationRunner {

	@Resource
	private Map<String, DefaultMQPushConsumer> defaultMQPushConsumerMap;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		if (MapUtil.isEmpty(defaultMQPushConsumerMap)) {
			return;
		}
		defaultMQPushConsumerMap.forEach((beanName, consumer) -> {
			try {
				if (Objects.isNull(consumer)) {
					return;
				}
				consumer.start();
			} catch (MQClientException e) {
				log.warn( "MQ：starting {} is fail", beanName, e);
			}
		});
	}
}
